//
//  CYLaunchAdViewController.m
//  cyjf
//
//  Created by tauCross on 2017/8/29.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYLaunchAdViewController.h"

#import "CYGlobalTimerManager.h"



@interface CYLaunchAdViewController ()

@property(nonatomic, strong)UIImageView *launchImageView;
@property(nonatomic, strong)UIImageView *adImageView;
@property(nonatomic, strong)UIButton *adButton;
@property(nonatomic, strong)UIButton *ignoreButton;

@property(nonatomic, assign)NSInteger countDown;

@property(nonatomic, strong)NSTimer *timer;

@end


@implementation CYLaunchAdViewController

- (void)dealloc
{
    DLog(@"Launch Ad Dealloc");
}

- (void)loadUI
{
    self.tableView.hidden = YES;
    
    NSString *launchImage = nil;
    NSArray *imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for(NSDictionary *dict in imagesDict)
    {
        if(CGSizeEqualToSize(CGSizeFromString(dict[@"UILaunchImageSize"]), [[UIScreen mainScreen] bounds].size))
        {
            launchImage = dict[@"UILaunchImageName"];
            break;
        }
    }
    if(launchImage)
    {
        self.launchImageView = [[UIImageView alloc] initWithImage:CYImage(launchImage)];
        [self.contentView addSubview:self.launchImageView];
    }
    else
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    
    self.adImageView = [[UIImageView alloc] initWithImage:self.object.image];
    self.adImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:self.adImageView];
    
    self.adButton = [[UIButton alloc] init];
    [self.adButton addTarget:self action:@selector(onAdButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.adButton];
    
    self.ignoreButton = [[UIButton alloc] init];
    self.ignoreButton.size = CGSizeMake(60, 25);
    [self.ignoreButton addTarget:self action:@selector(onIgnoreButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    self.ignoreButton.backgroundColor = HEXCOLORA(@"000000", 0.3);
    self.ignoreButton.layer.masksToBounds = YES;
    self.ignoreButton.layer.cornerRadius = self.ignoreButton.height / 2;
    self.ignoreButton.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [self.ignoreButton setTitleColor:HEXCOLOR(@"FFFFFF") forState:UIControlStateNormal];
    [self.contentView addSubview:self.ignoreButton];
}

- (void)layoutUI
{
    self.launchImageView.frame = self.contentView.bounds;
    
    self.adImageView.frame = self.contentView.bounds;
    self.adImageView.height = self.adImageView.height - 100 - ((SCREEN_HEIGHT == 812.0) ? 40 : 0);
    
    self.adButton.frame = self.contentView.bounds;
    
    self.ignoreButton.top = 30;
    self.ignoreButton.right = self.contentView.width - 21;
}

- (void)loadData
{
    self.countDown = 3;
    [self.ignoreButton setTitle:[NSString stringWithFormat:@"跳过 %li", (long)self.countDown] forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
    
}

- (void)countDownAction
{
    if(self.countDown > 0)
    {
        self.countDown --;
        [self.ignoreButton setTitle:[NSString stringWithFormat:@"跳过 %li", (long)self.countDown] forState:UIControlStateNormal];
    }
    else
    {
        [self noAction];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)noAction
{
    [self.timer invalidate];
    if(self.complete)
    {
        self.complete(nil);
    }
}

#pragma mark - Control Events
- (void)onAdButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0001];
    [self.timer invalidate];
    if(self.complete)
    {
        self.complete(self.object.model.action);
    }
}

- (void)onIgnoreButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0000];
    [self noAction];
}

@end
